package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.activitys.RemoteActivity;
import com.chuanye.mydrawable.beans.SupportUserBean;

import java.util.HashMap;
import java.util.List;

/**
 * Created by lsef on 2019/2/14.
 */

public class SupportUserTwoAdapter extends BaseAdapter{
    private Context mContext;
    private List<SupportUserBean.ObjBean.ValueBean> valueBeanList;

    private List<SupportUserBean.ObjBean.ValueBean> valueBeanList2;
    private boolean flag = false;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    public SupportUserTwoAdapter(Context mContext, List<SupportUserBean.ObjBean.ValueBean> valueBeanList,HashMap<Integer,Boolean> isSelected,List<SupportUserBean.ObjBean.ValueBean> valueBeanList2) {
        this.mContext = mContext;
        this.valueBeanList = valueBeanList;
        this.isSelected = isSelected;
        this.valueBeanList2 = valueBeanList2;
        Log.i("打印flag：","初始化数据"+flag);
        if (!flag){
            // 初始化数据
            initDate();
            Log.i("打印：","初始化数据");
            flag = true;
        }

    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < valueBeanList.size(); i++) {
            getIsSelected().put(i, true);
        }
    }

    @Override
    public int getCount() {
        return valueBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return valueBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.support_itme,null);
            holder = new ViewHolder();
            holder.support_item_tv = view.findViewById(R.id.support_item_tv);
            holder.support_item_iv = view.findViewById(R.id.support_item_iv);
            holder.support_item_ll = view.findViewById(R.id.support_item_ll);
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        holder.support_item_tv.setText(valueBeanList.get(i).getRealName());
        if (valueBeanList2 != null && valueBeanList2.size() > 0){
            for (int f = 0; f < valueBeanList2.size(); f ++){
                Log.i("打印：",valueBeanList2.get(f).getRealName());
                if (valueBeanList2.get(f).getRealName().equals(valueBeanList.get(i).getRealName())){
                    isSelected.put(i, true);
                }else {
                    //isSelected.put(i, false);
                }
            }
        }
        holder.support_item_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("点击了：",""+valueBeanList.get(i).getRealName());

                if (isSelected.get(i)) {
                    Log.i("点击了1：",""+isSelected.get(i));
                    isSelected.put(i, false);
                    setIsSelected(isSelected);
                    /*if (RemoteActivity.supportList2 !=null && RemoteActivity.supportList2.size() > 0){
                        for(int k = 0 ; k < RemoteActivity.supportList2.size(); k++){
                            if (RemoteActivity.supportList2.get(k).getSupportName().equals(objBeanList.get(i).getSupportName())){
                                RemoteActivity.supportList2.remove(k);
                                RemoteActivity.recyclerViewAdapter.notifyDataSetChanged();
                            }
                        }
                    }*/
                } else {
                    Log.i("点击了2：",""+isSelected.get(i));
                    isSelected.put(i, true);
                    setIsSelected(isSelected);
                    /*RemoteActivity.supportList2.add(objBeanList.get(i));
                    RemoteActivity.recyclerViewAdapter.notifyDataSetChanged();
                    RemoteActivity.crreremot_remoteexternal_tv.setVisibility(View.GONE);
                    RemoteActivity.crreremot_remoteexternal_rv.setVisibility(View.VISIBLE);*/
                }
                notifyDataSetChanged();
            }
        });

        // 根据isSelected来设置checkbox的选中状况
        if (getIsSelected().get(i)){
            holder.support_item_iv.setVisibility(View.VISIBLE);
            Log.i("点击了1222：",""+getIsSelected().get(i));
        }else {
            holder.support_item_iv.setVisibility(View.GONE);
            Log.i("点击了1333：",""+getIsSelected().get(i));
        }
        return view;
    }

    private static class ViewHolder{
        TextView support_item_tv;
        ImageView support_item_iv;
        LinearLayout support_item_ll;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        SupportUserTwoAdapter.isSelected = isSelected;
    }
}
